<template>
  <el-dialog v-if="load" :title="form.pa_id ? '编辑' : '添加'" v-model="visible" align-center width="800px"
    @close="handleClose" :close-on-click-modal="false">
    <el-form ref="formRef" :model="form" :rules="rules" :validate-on-rule-change="false" :label-width="140"
      class="w-11/12">
      <el-form-item label="平台类型" prop="platform_type">
        <el-select v-model="form.platform_type" class="w-full" clearable placeholder="请选择">
          <el-option v-for="(d, i) in options.platform_type" :key="i" :label="d.label" :value="d.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账号名称" prop="account_name">
        <el-input v-model="form.account_name" type="textarea" placeholder="账号名称" :maxlength="200" />
      </el-form-item>
      <el-form-item label="采购人员" prop="purchase_account_id">
        <el-select v-model="form.purchase_account_id" class="w-full" clearable placeholder="请选择"
          @change="val => (form.purchase_account = purchaseOptions.find(d => d.value === val).label)">
          <el-option v-for="(d, i) in purchaseOptions" :key="i" :label="d.label" :value="d.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="下单支付宝账号" prop="alipay_account_id">
        <el-select v-model="form.alipay_account_id" class="w-full" clearable placeholder="请选择">
          <el-option v-for="(d, i) in alipayOptions" :key="i" :label="d.label" :value="d.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="账号类型" prop="account_type">
        <el-select v-model="form.account_type" class="w-full" clearable placeholder="请选择">
          <el-option v-for="(d, i) in options.account_type" :key="i" :label="d.label" :value="d.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="公司主体" prop="company_id">
        <el-select v-model="form.company_id" class="w-full" clearable placeholder="请选择">
          <el-option v-for="(d, i) in options.company_id" :key="i" :label="d.label" :value="d.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="使用状态" prop="status">
        <el-select v-model="form.status" class="w-full" clearable placeholder="请选择">
          <el-option v-for="(d, i) in options.status" :key="i" :label="d.label" :value="d.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" :loading="loadings.create" @click="create(formRef)">保 存</el-button>
    </template>
  </el-dialog>
</template>

<script setup>
import { reactive } from 'vue';
import api from '../api'
import { ElMessage } from 'element-plus'
const props = defineProps({
  options: { type: Object, default: () => ({}) },
})
const emit = defineEmits(['close', 'success'])

const load = ref(false)
const visible = ref(false)
const alipayOptions = ref([])
const purchaseOptions = ref([{ label: '人员1', value: '1' }])
let loadings = reactive({})

// 表单处理
const formRef = ref()
const form = ref({})
const rules = reactive({
  platform_type: [{ required: true, message: '不能为空', trigger: 'change' }],
  account_name: [{ required: true, message: '不能为空' }],
  purchase_account_id: [{ required: true, message: '不能为空', trigger: 'change' }],
  company_id: [{ required: true, message: '不能为空', trigger: 'change' }],
  status: [{ required: true, message: '不能为空', trigger: 'change' }],
})


// 打开弹窗
const open = async (id) => {
  if (!load.value) {
    load.value = true
    api.getAlipay().then(res => {
      alipayOptions.value = res.data.map(d => ({ label: d.alipay_account, value: d.aa_id }))
    })
    await 1
  }
  // 编辑
  if (id) {
    const res = await api.detail({ pa_id: id })
    const { pa_id, account_name, platform_type, purchase_account_id, purchase_account, account_type, alipay_account_id, company_id, status } = res.data
    form.value = { pa_id, account_name, platform_type, purchase_account_id, purchase_account, account_type, alipay_account_id, company_id, status }
  } else { //新增
    form.value = {
      pa_id: '',
      account_name: '',
      platform_type: undefined,
      purchase_account_id: '',
      purchase_account: '',
      account_type: '',
      alipay_account_id: '',
      company_id: '',
      status: ''
    }
  }
  loadings = reactive({})
  visible.value = true
  setTimeout(() => {
    formRef?.value.clearValidate()
  }, 100);
}
// 关闭弹窗
const close = () => {
  visible.value = false
}

const handleClose = () => {
  emit('close')
}
// 提交
const create = async (formRef) => {
  const validator = await formRef.validate()
  if (!validator) return
  loadings.create = true
  api[form.value.pa_id ? 'edit' : 'create'](form.value).then(res => {
    ElMessage.success('保存成功')
    emit('success')
    close()
  }).finally(() => {
    loadings.create = false
  })
}

defineExpose({ open })
</script>
